package controller;

import dao.LogDao;
import dao.SocketLogDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class LogSearch extends HttpServlet {
    private static final long serialVersionUID = -8187331283582013985L;
    private static LogDao logDao = new LogDao();
    private static SocketLogDao socketLogDao = new SocketLogDao();
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String startDate = request.getParameter("startDate");
            String endDate = request.getParameter("endDate");
            String startTime;
            String endTime;
            if(startDate==""||startDate.equals("")){
                startTime = null;
            }else{
                startTime = simpleDateFormat.format(simpleDateFormat.parse(startDate));
            }
            if(endDate==""||endDate.equals("")){
                endTime = null;
            }else{
                Calendar cal = Calendar.getInstance();
                cal.setTime(simpleDateFormat.parse(endDate));
                cal.add(Calendar.DATE,1);
                endTime = simpleDateFormat.format(cal.getTime());
            }
            String match = request.getParameter("content");
            if(match.equals("")||match==""){
                match = null;
            }
            Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
            Integer onePageCount = Integer.parseInt(request.getParameter("onePageCount"));
            List<Object> results = null;
            int pageNum = 0;
            //根据该字段来判断是否是平台用户行为日志的查询
            if(request.getParameter("operateLog").equals("1"))
            {
                results = logDao.logFulltextSearch(onePageCount, currentPage, match, startTime, endTime);
                pageNum = logDao.logFulltextSearchNum(onePageCount,currentPage,match,startTime,endTime);

            }
            else if(request.getParameter("operateLog").equals("0"))
            {
                results = socketLogDao.logFulltextSearch(onePageCount, currentPage, match, startTime, endTime);
                pageNum = socketLogDao.logFulltextSearchNum(onePageCount,currentPage,match,startTime,endTime);
            }
            if(results!=null)
            {
                JSONObject result = new JSONObject();
                JSONObject content = new JSONObject();
                content.put("logs", JSONArray.fromObject(results));
                content.put("pageNum", pageNum);
                result.put("result", content);
                response.getWriter().write(result.toString());
            }else{
                response.getWriter().write("error");
            }
        } catch (NumberFormatException | ParseException e) {
            e.printStackTrace();
            response.getWriter().write("error");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
